<template>
  <div>
    <div>
      <!-- 加载中 -->
      <div v-if="loading">Loading...</div>
      <!-- 无可用数据 -->
      <div v-if="show">
        <van-empty description="无可用数据">
          <van-button class="btn">健康科普</van-button>
          <van-button class="btn" @click="zhuan">咨询专家</van-button>
          <van-button class="btn" @click="gua">医院挂号</van-button>
          <van-button class="btn">线上购药</van-button>
          <span @click="feedback" class="feedback">我要反馈</span>
        </van-empty>
      </div>
      <!-- 有可用数据 -->
      <div v-else>
        <div class="nav-box">
          <!-- 相关科普 -->
          <div class="navtab" v-if="booknav.length !== 0">
            <div class="nav-zong">
              <h4>相关科普</h4>
              <div>查看更多></div>
            </div>
            <div v-for="(item, index) in booknav" :key="index" class="nav-for">
              <div class="titletext">{{ item.texttitle }}</div>
              <div class="wordtext">{{ item.textdescword }}</div>
              <div class="textdoc">
                <div>{{ item.textname }}</div>
                <div>{{ item.textcitys }}医院</div>
                <div>{{ item.textdoction }}</div>
                <div>{{ item.texttype }}</div>
              </div>
            </div>
          </div>
          <!-- 相关医生 -->
          <div class="physician-max" v-if="list.length !== 0">
            <div class="nav-zong">
              <h4>相关科普</h4>
              <div>查看更多></div>
            </div>
            <div class="physician" v-for="(item, index) in list" :key="index">
              <img :src="item.doctorimage" v-lazy="item.doctorimage" class="image" />
              <div class="physicians">
                <div class="warp1">
                  <h4>{{ item.doctorname }}</h4>
                  <div>{{ item.doctorstate }}</div>
                </div>
                <div>{{ item.doctorcity }} {{ item.doctoradministre }}</div>
                <div class="navs">
                  <div>图文咨询</div>
                  <div>电话咨询</div>
                  <div>视频咨询</div>
                </div>
                <div class="titletext">擅长:{{ item.doctordescword }}</div>
                <span class="reviews">好评率 <span class="color">{{ item.doctorgood }}%</span> | 锦旗 <span class="color">{{
                    item.doctortime }}</span> | 浏览人次 <span class="color">{{ item.doctorbrowse }}</span></span>
                <div class="num">
                  <div>￥{{ item.doctorprice }} <span>起</span></div>
                  <div class="doct" @click="doctor(item)">问医生</div>
                </div>
              </div>
            </div>
          </div>
          <!-- 相关医院 -->
          <div class="doction-box" v-if="doctlist.length !== 0">
            <div class="nav-zong">
              <h4>相关科普</h4>
              <div>查看更多></div>
            </div>
            <div v-for="(item, index) in doctlist" :key="index" class="doction-flex">
              <div class="doction-nav">
                <img src="../../assets/image/doctions.png" alt="" />
              </div>
              <div>
                <div>{{ item.nosocomium }}</div>
                <div class="doctions-node">
                  <div>{{ item.socolv }}</div>
                  <div>8:30放7天号源</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, defineProps } from 'vue';
import { useRouter } from 'vue-router';
// import axios from 'axios';
import axios from '@/utils/axios/axios';
import { useRoute } from 'vue-router';
const props = defineProps({
  item: String,
  is: Number
});

console.log(props);

// 获取路由信息
const route = useRoute();
// 获取路由参数中的items
const items = ref(JSON.parse(route.query.items));
console.log(items);
// 定义一个变量，用于控制是否显示
const show = ref(false);
// 定义一个变量，用于存储书籍导航信息
const booknav = ref([]);
// 定义一个变量，用于存储搜索关键字
const SearchQuery = ref('');
// 定义一个变量，用于存储列表信息
const list = ref([]);
// 定义一个变量，用于存储医生列表信息
const doctlist = ref([]);
// 定义一个异步函数，用于获取书籍列表信息
const booklist = async () => {
  await axios.get('/api/bookslists').then((res) => {
    // 将搜索关键字设置为items的值
    SearchQuery.value = items.value;
    // 过滤出包含搜索关键字的书籍，并截取前两个
    booknav.value = res.data.res.filter((s) => s.texttitle.includes(SearchQuery.value)).splice(0, 2);
  });
};
// 跳转医生详情页面
const doctor = (item) => {
  // 将item转换为JSON字符串
  const items = JSON.stringify(item);
  // 跳转到askdoctor页面，并传递item参数
  router.push('/askdoctor?item=' + items)
}
// 调用booklist函数
booklist();
// 定义一个异步函数，用于获取医生列表信息
const userlist = async () => {
  await axios.get('/api/doctorlist').then((res) => {
    // 将搜索关键字设置为items的值
    SearchQuery.value = items.value;
    // 过滤出包含搜索关键字的医生，并截取前两个
    list.value = res.data.res.filter((s) => s.doctorname.includes(SearchQuery.value)).splice(0, 2);
  });
};
// 调用userlist函数
userlist();
// 定义一个异步函数，用于获取医生科室信息
const doctionkeys = async () => {
  await axios.get('/api/doctionkey').then((res) => {
    // 将搜索关键字设置为items的值
    SearchQuery.value = items.value;
    // 过滤出包含搜索关键字的医生科室，并截取前两个
    doctlist.value = res.data.res.filter((s) => s.nosocomium.includes(SearchQuery.value)).splice(0, 2);
  });
};
// 调用doctionkeys函数
doctionkeys();
// 设置一个定时器，3秒后判断booknav、list、doctlist是否为空，如果为空，则将show设置为true
setTimeout(() => {
  if (booknav.value.length == 0 && list.value.length == 0 && doctlist.value.length == 0) {
    console.log(123);

    show.value = true;
  }
}, 3000);
// 定义一个变量，用于存储数据
const data = ref([]);
// 定义一个变量，用于控制加载状态
// 获取路由实例
const loading = ref(true);
const router = useRouter();
onMounted(async () => {
  try {
    // const response = await axios.get('https://api.example.com/data');
    // data.value = response.data;
  } catch (error) {
    console.error('Error fetching data:', error);
  } finally {
    loading.value = false;
  }
});
// 跳转专家咨询
const zhuan = () => {
  router.push('/layout/popular');
};
// 医院挂号
const gua = () => {
  router.push('/layout/appointment');
};
</script>

<style scoped lang="scss">
.nav-box {
  width: 100%;
  height: 140vh;
  background: #f5f5f5;
}

.list_item {
  width: 90%;
  height: 18vh;
  background: #d2e9eb;
  border-radius: 10px;
}

.btn {
  width: 80px;
  height: 30px;
  float: left;
  border: none;
  border: 1px solid blue;
  color: blue;
  border-radius: 10px;
  font-size: 12px;
  margin-left: 5px;
}

.feedback {
  color: blue;
  font-size: 14px;
  border-bottom: 2px solid blue;
  position: absolute;
  top: 80%;
  left: 42%;
}

.list {
  text-align: center;
}

.list_item {
  margin-left: 5%;
  margin-top: 2%;
  overflow: hidden;
}

.list_item-head {
  width: 100%;
  height: 6vh;
  font-size: 20px;
  line-height: 6vh;
  padding-left: 5px;
}

.list_item_head {
  width: 20%;
  height: 80%;
  margin-top: 10px;
  margin-left: 10px;
  border-radius: 150px;
  float: left;
}

.list_item_div {
  width: 100%;
  height: 12vh;
}

.list_item_head_txt {
  float: right;
  color: #ccc;
  padding-right: 5px;
  font-size: 14px;
}

.list_item_footer {
  font-size: 18px;
  padding-top: 15px;
  padding-left: 10px;
}

.navtab {
  width: 93%;
  height: 237px;
  border-radius: 10px;
  margin: auto;
  background: #ffffff;

  .nav-zong {
    font-size: 0.4rem;

    h4 {
      float: left;
      margin: 12px;
    }

    >div {
      float: right;
      margin: 12px;
    }
  }

  .nav-for {
    clear: both;
    width: 95%;
    margin: auto;
    height: 90px;
    line-height: 20px;
    margin-bottom: 10px;

    .titletext {
      font-size: 0.36rem;
    }

    .wordtext {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      /* 限制显示的行数 */
      -webkit-box-orient: vertical;
    }
  }

  .textdoc {
    height: 30px;
    line-height: 30px;
    display: flex;
    justify-content: space-between;

    >div:nth-child(4) {
      color: #00be8c;
    }
  }
}

.physician-max {
  width: 93%;
  height: 416px;
  margin: auto;
  margin-top: 10px;
  overflow: hidden;
  background: #fff;
  border-radius: 10px;

  .nav-zong {
    font-size: 0.4rem;

    h4 {
      float: left;
      margin: 12px;
    }

    >div {
      float: right;
      margin: 12px;
    }
  }

  .physician {
    display: flex;
    clear: both;
    font-size: 0.3rem;
    margin: 5px;

    .image {
      width: 50px;
      height: 50px;
      border-radius: 50px;
    }

    .physicians {
      width: 250px;
      height: 190px;

      // margin-left: 2px;
      .titletext {
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        /* 限制显示的行数 */
        -webkit-box-orient: vertical;
      }

      .num {
        clear: both;
        display: flex;
        height: 50px;
        align-items: center;
        font-size: 0.4rem;
        justify-content: space-between;
        color: #fa6400;
        margin: 10px;

        .doct {
          width: 70px;
          height: 34px;
          text-align: center;
          line-height: 34px;
          background: #1870f4;
          color: #fff;
          border-radius: 10px;
        }

        >div>span {
          color: #ccc;
          font-size: 0.3rem;
        }
      }

      .reviews {
        margin-top: 10px;
        font-size: 0.3rem;
        float: left;

        .color {
          color: #fa6400;
        }
      }

      .navs {
        display: flex;

        >div:nth-child(1) {
          width: 75px;
          height: 30px;
          border-radius: 10px;
          margin: 5px;
          line-height: 30px;
          text-align: center;
          background: #e7f0ff;
          color: #5892ff;
        }

        >div:nth-child(2) {
          width: 75px;
          height: 30px;
          margin: 5px;
          border-radius: 10px;
          line-height: 30px;
          text-align: center;
          color: #20c0af;
          background: #e4f7f2;
        }

        >div:nth-child(3) {
          width: 75px;
          height: 30px;
          margin: 5px;
          border-radius: 10px;
          line-height: 30px;
          text-align: center;
          color: #67cff9;
          background: #e5f7fd;
        }
      }

      .warp1 {
        display: flex;

        >h4 {
          margin: 0px 5px;
        }

        >div {
          margin-left: 10px;
        }
      }
    }
  }
}

.doction-box {
  width: 93%;
  margin: auto;
  height: 210px;
  background: #fff;
  margin-top: 10px;

  .nav-zong {
    font-size: 0.4rem;

    h4 {
      float: left;
      margin: 12px;
    }

    >div {
      float: right;
      margin: 12px;
    }
  }

  .doction-flex {
    display: flex;
    align-items: center;
    clear: both;

    .doctions-node {
      display: flex;
      align-items: center;

      >div:nth-child(1) {
        width: 55px;
        height: 20px;
        line-height: 20px;
        background: #dfebff;
        text-align: center;
        border-radius: 10px;
        overflow: hidden;
      }

      >div:nth-child(2) {
        color: #ccc;
      }
    }

    .doction-nav {
      clear: both;
      margin: 10px;
      width: 55px;
      text-align: center;
      border-radius: 4px;
      height: 50px;
      border: 1px solid #ccc;
    }
  }
}
</style>